package File;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-04-04
 * Time: 14:39
 */
public class Demo17 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件（绝对路径或相对路径）");
        String sourceFile = scanner.nextLine();
        File file = new File(sourceFile);
        if(!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if(!file.isFile()) {
            System.out.println("不是普通文件");
            return;
        }
        System.out.println("请输入要复制到的目标路径");
        String destPath = scanner.nextLine();
        File destFile = new File(destPath);
        if(destFile.exists()) {
            if(destFile.isDirectory()) {
                System.out.println("目标文件是目录文件");
                return;
            }
            if(destFile.isFile()) {
                System.out.println("目标文件已存在是否要覆盖(y/n)");
                String in = scanner.nextLine();
                if(!in.equalsIgnoreCase("Y")) {
                    System.out.println("停止复制");
                    return;
                }
            }
        }
        try (InputStream inputStream = new FileInputStream(sourceFile)){
            try (OutputStream outputStream = new FileOutputStream(destFile)){
                byte[] bytes = new byte[1024];
                int len;
                while(true) {
                    len = inputStream.read(bytes);
                    if (len == -1) {
                        break;
                    }
                    outputStream.write(bytes,0,len);
                }
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("复制完成");
    }
}
